/*! ----------------------------------------------------------------------------
 * @file	port.h
 * @brief	HW specific definitions and functions for portability
 *
 * @attention
 *
 * Copyright 2013 (c) DecaWave Ltd, Dublin, Ireland.
 *
 * All rights reserved.
 *
 * @author DecaWave
 */


#ifndef PORT_H_
#define PORT_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "stm32f10x.h"
#include "stm32f10x_exti.h"
#include "stm32f10x_spi.h"
#include "stm32f10x_rtc.h"
#include "deca_device_api.h"

extern int writetospi_serial(uint16 headerLength,
                             const uint8 *headerBuffer,
                             uint32 bodylength,
                             const uint8 *bodyBuffer);

extern int readfromspi_serial(uint16	headerLength,
                              const uint8 *headerBuffer,
                              uint32 readlength,
                              uint8 *readBuffer );

#define writetospi  writetospi_serial
#define readfromspi readfromspi_serial

#define SPIx_PRESCALER				SPI_BaudRatePrescaler_8

#define SPIx							SPI1
#define SPIx_GPIO					GPIOA
#define SPIx_CS						GPIO_Pin_4
#define SPIx_CS_GPIO			GPIOA
#define SPIx_SCK					GPIO_Pin_5
#define SPIx_MISO					GPIO_Pin_6
#define SPIx_MOSI					GPIO_Pin_7

															
#define DW1000_RSTn					GPIO_Pin_0
#define DW1000_RSTn_GPIO			GPIOB
															
															
															
#define DECAIRQ                     GPIO_Pin_4
#define DECAIRQ_GPIO                GPIOC
#define DECAIRQ_EXTI                EXTI_Line4
#define DECAIRQ_EXTI_PORT           GPIO_PortSourceGPIOC
#define DECAIRQ_EXTI_PIN            GPIO_PinSource4
#define DECAIRQ_EXTI_IRQn           EXTI4_IRQn
#define DECAIRQ_EXTI_USEIRQ         ENABLE
#define DECAIRQ_EXTI_NOIRQ          DISABLE


#define port_SPIx_busy_sending()		(SPI_I2S_GetFlagStatus((SPIx),(SPI_I2S_FLAG_TXE))==(RESET))
#define port_SPIx_no_data()				(SPI_I2S_GetFlagStatus((SPIx),(SPI_I2S_FLAG_RXNE))==(RESET))
#define port_SPIx_send_data(x)			SPI_I2S_SendData((SPIx),(x))
#define port_SPIx_receive_data()		SPI_I2S_ReceiveData(SPIx)
#define port_SPIx_disable()				SPI_Cmd(SPIx,DISABLE)
#define port_SPIx_enable()              SPI_Cmd(SPIx,ENABLE)
#define port_SPIx_set_chip_select()		GPIO_SetBits(SPIx_CS_GPIO,SPIx_CS)
#define port_SPIx_clear_chip_select()	GPIO_ResetBits(SPIx_CS_GPIO,SPIx_CS)

ITStatus EXTI_GetITEnStatus(uint32_t x);

#define port_GetEXT_IRQStatus()             EXTI_GetITEnStatus(DECAIRQ_EXTI_IRQn)
#define port_DisableEXT_IRQ()               NVIC_DisableIRQ(DECAIRQ_EXTI_IRQn)
#define port_EnableEXT_IRQ()                NVIC_EnableIRQ(DECAIRQ_EXTI_IRQn)
#define portGetTickCount() 			portGetTickCnt()

void reset_DW1000(void);
void dwm1000_peripherals_init (void);
void SPI_ChangeRate(uint16_t scalingfactor);
void SPI_ConfigFastRate(uint16_t scalingfactor);
void spi_set_rate_low (void);
void spi_set_rate_high (void);
unsigned long portGetTickCnt(void);


#ifdef __cplusplus
}
#endif

#endif /* PORT_H_ */
